package com.moyq5.mvc.maker.test.bean.entity;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.maker.test.bean.enums.ChannelQrcodeOrderAmountType;
import com.moyq5.mvc.maker.test.bean.enums.ChannelQrcodeOrderStatus;

/**
 * 个人/企业收款码使用（交易）记录
 * @author Moyq5
 * @date 2018年9月6日
 */
@Entity
@Table(name = "tra_channel_qrcode_order")
@DynamicUpdate
public class TraChannelQrcodeOrder implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	/**
	 * 交易ID
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "order_id")
	private Long orderId;
	/**
	 * 订单号
	 */
	@Column(name = "order_no", nullable = false, length = 30, unique = true)
	private String orderNo;
	/**
	 * 收款码
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "qrcode_id", nullable = false)
	private TraChannelQrcode qrcode;
	/**
     * 收款金额
     */
    @Column(name = "amount", nullable = false, precision = 10, scale = 2)
    private BigDecimal amount;
    /**
	 * 收款金额来源：收款码设定的收款金额，用户自定义金额
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "amount_type", nullable = false, precision = 1)
	private ChannelQrcodeOrderAmountType amountType;
	/**
	 * 状态，收款码使用状态，重复扫码、扫过期码视为无效
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", nullable = false, columnDefinition="int(1) not null default '0'")
	private ChannelQrcodeOrderStatus status;
	/**
	 * 过期时间，超过这个时间点扫码视为无效
	 */
	@Column(name = "expire_time", nullable = false, columnDefinition = "datetime")
	private Date expireTime;
	/**
	 * (记录)添加时间
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	@PrePersist
	public void prePersist() {
		if (null == orderNo) {
			String prefix = qrcode.getQrcodeId().toString();
			if (prefix.length() > 8) {
				prefix = prefix.substring(prefix.length() - 8, prefix.length());
			} else if (prefix.length() < 8) {
				prefix += (Math.round(Math.random()*(Math.pow(10, 8 - prefix.length()) - Math.pow(10, 8 - prefix.length() -1) - 1) + Math.pow(10, 8 - prefix.length())));
			}
			orderNo = prefix + Long.parseLong(new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date()) + Math.round(Math.random()*(1e3-1e2-1) + 1e3));
			
		}
		if (null == addTime) {
			addTime = new Date();
		}
		if (null == expireTime) {
			expireTime = new Date(System.currentTimeMillis() + (long)6e4);// 默认为1分钟内扫码有效
		}
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public TraChannelQrcode getQrcode() {
		return qrcode;
	}

	public void setQrcode(TraChannelQrcode qrcode) {
		this.qrcode = qrcode;
	}

	public ChannelQrcodeOrderStatus getStatus() {
		return status;
	}

	public void setStatus(ChannelQrcodeOrderStatus status) {
		this.status = status;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}

	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public ChannelQrcodeOrderAmountType getAmountType() {
		return amountType;
	}

	public void setAmountType(ChannelQrcodeOrderAmountType amountType) {
		this.amountType = amountType;
	}

}
